package com.ycsj.goldmedalnewconcept.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.flyco.dialog.listener.OnBtnLeftClickL;
import com.flyco.dialog.listener.OnBtnRightClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.bean.ZhiboInfo;
import com.ycsj.goldmedalnewconcept.bean.ZhiboListResponse;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//直播列表
public class ZhiboListActivity extends Activity {

	private ImageView iv_back;
	private ListView lv_zhibo;
	private List<ZhiboInfo> list = new ArrayList<ZhiboInfo>();
	private List<ZhiboInfo>  filterlist = new ArrayList<ZhiboInfo>();
	private MyAdapter mAdapter;
	private Intent intent;
	private String type;
	private String role;
	private TextView tv_nodata;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_zhibolist);

		role = SPUtil.getString(ZhiboListActivity.this, "role", "");
		initView();
		setOnListener();
		initDate();
	}

	private void setOnListener() {
		// TODO Auto-generated method stub
		iv_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

		lv_zhibo.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				
//				//测试
//				intent = new Intent(getBaseContext(), ZhiboActivity.class);
//				intent.putExtra("p", position);
//				intent.putExtra("v_id", filterlist.get(position).v_id);
//				startActivity(intent);

				
				if("2".equals(filterlist.get(position).state)&&!TextUtils.isEmpty(filterlist.get(position).url)){
					intent = new Intent(getBaseContext(), ZhiboOverActivity.class);
					intent.putExtra("url", filterlist.get(position).url);
					intent.putExtra("v_id", filterlist.get(position).v_id);
					intent.putExtra("title", filterlist.get(position).title);
					intent.putExtra("time", filterlist.get(position).playtime.substring(0, 16));
					intent.putExtra("picurl", filterlist.get(position).pic);
					startActivity(intent);
				}else if("2".equals(filterlist.get(position).state)&&TextUtils.isEmpty(filterlist.get(position).url)){
					
					dialog1(position);	//已结束提示框
					
				}else if("0".equals(filterlist.get(position).state)){
					
					dialog(position);	//未开始提示框
					
				}else{
					intent = new Intent(getBaseContext(), ZhiboActivity.class);
					intent.putExtra("p", position);
					intent.putExtra("v_id", filterlist.get(position).v_id);
					startActivity(intent);
				}
				
				
			}

			private void dialog1(int position) {
				final NormalDialog dialog = new NormalDialog(
						ZhiboListActivity.this);
				dialog.content("直播已结束，程序GG正在加急审核，请稍后观看录播（一般会审核1-3个工作日）")
					.btnText("取消", "确定")
						.style(NormalDialog.STYLE_TWO)//
						.titleTextSize(23)//
						.show();
				dialog.setCanceledOnTouchOutside(false);
				//取消
				dialog.setOnBtnLeftClickL(new OnBtnLeftClickL() {

					@Override
					public void onBtnLeftClick() {
						dialog.dismiss();
					}
				});
				
				dialog.setOnBtnRightClickL(new OnBtnRightClickL() {

					@Override
					public void onBtnRightClick() {
						dialog.dismiss();
					}
				});
			}

			private void dialog(int position) {
				final NormalDialog dialog = new NormalDialog(
						ZhiboListActivity.this);
				dialog.content(list.get(position).title+",直播时间为"+list.get(position).playtime.substring(0, 16)+",请稍后观看")
					.btnText("取消", "确定")
						.style(NormalDialog.STYLE_TWO)//
						.titleTextSize(23)//
						.show();
				dialog.setCanceledOnTouchOutside(false);
				//取消
				dialog.setOnBtnLeftClickL(new OnBtnLeftClickL() {

					@Override
					public void onBtnLeftClick() {
						dialog.dismiss();
					}
				});
				
				dialog.setOnBtnRightClickL(new OnBtnRightClickL() {

					@Override
					public void onBtnRightClick() {
						dialog.dismiss();
					}
				});
			}
		});
	}

	private void initView() {
		// TODO Auto-generated method stub
		iv_back = (ImageView) findViewById(R.id.iv_back);
		lv_zhibo = (ListView) findViewById(R.id.lv_zhibo);
		tv_nodata = (TextView) findViewById(R.id.tv_nodata);
	}

	private void initDate() {
		OkHttpClient mOkHttpClient = new OkHttpClient();
		// 创建一个Request
		FormEncodingBuilder builder = new FormEncodingBuilder();
		builder.add("type", "110");
		// http://118.190.88.18:8080/ycsj_count/MessageCenter?type=110
		final Request request = new Request.Builder()
				.url(Constant.ycsj_count_MessageCenter)
				//.url("http://pm.moregolden.com/ycsj_count/MessageCenter")
				.post(builder.build()).build();
		Call call = mOkHttpClient.newCall(request);
		call.enqueue(callBack);
	}

	class ViewHolder {
		public ImageView iv_img;
		public TextView tv_time;
		public TextView tv_title;
		public TextView tv_speaker;
		public TextView tv_state;
	}

	class MyAdapter extends BaseAdapter {

		List<ZhiboInfo> list1 = new ArrayList<ZhiboInfo>();
		
		private LayoutInflater mInflater = null;

		private MyAdapter(Context context,List<ZhiboInfo> filterlist) {
			this.mInflater = LayoutInflater.from(context);
			this.list1 = filterlist;
		}

		@Override
		public int getCount() {
			return list1.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.list_zhibo, null);

				holder.iv_img = (ImageView) convertView
						.findViewById(R.id.iv_img);
				holder.tv_time = (TextView) convertView
						.findViewById(R.id.tv_time);
				holder.tv_title = (TextView) convertView
						.findViewById(R.id.tv_title);
				holder.tv_speaker = (TextView) convertView
						.findViewById(R.id.tv_speaker);
				holder.tv_state = (TextView) convertView
						.findViewById(R.id.tv_state);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			Picasso.with(ZhiboListActivity.this)
			.load(list1.get(position).pic)
			.error(R.drawable.applogo).into(holder.iv_img);
			
			holder.tv_time.setText(list1.get(position).playtime);
			holder.tv_title.setText(list1.get(position).title);
			holder.tv_speaker.setText("主讲人：" + list1.get(position).speaker);

			if ("0".equals(list1.get(position).state)) {
				holder.tv_state.setText("未开始");
				holder.tv_state.setBackgroundResource(R.drawable.shape_red_5);
			} else if ("1".equals(list1.get(position).state)) {
				holder.tv_state.setText("直播中");
				holder.tv_state.setBackgroundResource(R.drawable.shape_blue);
			} else if ("2".equals(list1.get(position).state)) {
				holder.tv_state.setText("已结束");
				holder.tv_state.setBackgroundResource(R.drawable.shape_gary_bg);
			}

			return convertView;
		}
	}

	Handler handler = new Handler();
	Callback callBack = new Callback() {

		

		@Override
		public void onResponse(Response arg0) throws IOException {
			
			String json = arg0.body().string();
			
			
			
			Log.e("test", json);
			Gson gson = new Gson();
			list.removeAll(list);

			if ("{\"state\":-1}".equals(json)) {
				handler.postDelayed(new Runnable() {
					@Override
					public void run() {

					}
				}, 0);
			}

			if (!("{\"state\":-1}").equals(json)) {
				ZhiboListResponse fromJson = gson.fromJson(json,
						ZhiboListResponse.class);
				if ("0".equals(fromJson.state)) {
					handler.postDelayed(new Runnable() {
						@Override
						public void run() {

						}
					}, 0);
				}

				if (fromJson != null || fromJson.state != "-1") {
					list = fromJson.list;
					
					for (int i = 0; i < list.size(); i++) {
						
						if("1".equals(list.get(i).Android)){
							
							if("1".equals(role)){
								if("1".equals(list.get(i).L_PA)){	//家长可看
									filterlist.add(list.get(i));
								}
							}else if("2".equals(role)){
								if("1".equals(list.get(i).L_TE)){	//教师可看
									filterlist.add(list.get(i));
								}
							}else if("3".equals(role)){
								if("1".equals(list.get(i).L_PR)){	//校长可看
									filterlist.add(list.get(i));
								}
							}
//							else if("0".equals(role)){
//								if("1".equals(list.get(i).L_ST)){
//									filterlist.add(list.get(i));
//								}
//							}
						}
					}
				}

				handler.postDelayed(new Runnable() {

					@Override
					public void run() {

						if (filterlist != null&&filterlist.size()!=0) {
							mAdapter = new MyAdapter(ZhiboListActivity.this,filterlist);
							lv_zhibo.setAdapter(mAdapter);

						}else{
							tv_nodata.setVisibility(tv_nodata.VISIBLE);
						}
					}
				}, 0);
			}
		}

		@Override
		public void onFailure(Request arg0, IOException arg1) {

		}
	};

}
